//
//  ViewController.m
//  03-18-30302CALayer练习
//
//  Created by student on 16/3/20.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,weak )CALayer *layer;
@end

@implementation ViewController

//30302练习:
//1 (touchesBegan中)当双击时，在触摸位置新建的一个随机颜色固定大小的CALayer对象的方块
//2 添加上下左右手势控制这个layer上下左右滑动,比如向右，控制这些新建的CAlayer移动到屏幕最右侧，但不要超出屏幕。
//3 (touchesBegan中)如果点中某个方块，方块设置阴影颜色(亮一点的颜色)、阴影宽度，之前点选的块取消阴影
//*4 块之间不能重叠


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //右
    UISwipeGestureRecognizer *swipeRR=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDo:)];
    swipeRR.direction=UISwipeGestureRecognizerDirectionRight;
    
    [self.view addGestureRecognizer:swipeRR];
    
    //左
    UISwipeGestureRecognizer *swipeLR=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDo:)];
    swipeLR.direction=UISwipeGestureRecognizerDirectionLeft;
    
    [self.view addGestureRecognizer:swipeLR];
    
    //上
    UISwipeGestureRecognizer *swipeUR=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDo:)];
    swipeUR.direction=UISwipeGestureRecognizerDirectionUp;
    
    [self.view addGestureRecognizer:swipeUR];
    
    //下
    UISwipeGestureRecognizer *swipeDR=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDo:)];
    swipeDR.direction=UISwipeGestureRecognizerDirectionDown;
    
    [self.view addGestureRecognizer:swipeDR];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    
    CGPoint center=[touch locationInView:self.view];
    
    if (touch.tapCount==2)
    {
        //新建CALayer对象
        CALayer *layer=[CALayer layer];
        layer.backgroundColor=[UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1].CGColor;
        layer.frame=CGRectMake(center.x, center.y, 80, 80);
        
        self.layer=layer;
        BOOL yesOrNo=YES;
        for (CALayer *layerold in self.view.layer.sublayers) {
            if (self.layer.position.x>=40&&self.layer.position.y>=40&&self.layer.position.x<=280&&self.layer.position.y<=528&& fabs(self.layer.position.x-layerold.position.x)>=80|fabs(self.layer.position.y-layerold.position.y)>=80) {
                yesOrNo=yesOrNo&YES;
            }
            else{
                yesOrNo=yesOrNo&NO;
            }
        }
        if (yesOrNo) {
            [self.view.layer addSublayer:self.layer];
            
        }
        
    }
    if (touch.tapCount==1) {
        for (CALayer *layer in self.view.layer.sublayers) {
            if (fabs(layer.position.x-center.x)<40&fabs(layer.position.y-center.y)<40) {
                layer.shadowColor=[UIColor redColor].CGColor;
                layer.shadowRadius=15;
                layer.shadowOpacity=0.8f;
            }
            else{
                layer.shadowOpacity=0;
                
            }
        }
    }
    
    
    
}
-(void)swipeDo:(UISwipeGestureRecognizer *)g
{
    if (g.direction==UISwipeGestureRecognizerDirectionLeft) {
        for (CALayer *layer in self.view.layer.sublayers) {
//            CALayer *layerXMin=self.view.layer.sublayers[0];
//            CALayer *layerYmin=self.view.layer.sublayers[0];
//            
//            if (layerXMin.position.x<layer.position.x) {
//                layerXMin=layer;
//            
//            }
//            
            layer.frame=CGRectOffset(layer.frame, -20, 0);
            if (layer.position.x<40) {
                layer.position=CGPointMake(40, layer.position.y);
            }
        }
    }
   else if (g.direction==UISwipeGestureRecognizerDirectionRight) {
        
        for (CALayer *layer in self.view.layer.sublayers) {
            layer.frame=CGRectOffset(layer.frame, 20, 0);
            if (layer.position.x>280) {
                layer.position=CGPointMake(280, layer.position.y);
            }
        }
    }
   else if (g.direction==UISwipeGestureRecognizerDirectionUp) {
        
        for (CALayer *layer in self.view.layer.sublayers) {
            layer.frame=CGRectOffset(layer.frame, 0, -20);
            if (layer.position.y<40) {
                layer.position=CGPointMake(layer.position.x, 40);
            }
        }
    }
   else if (g.direction==UISwipeGestureRecognizerDirectionDown) {
        
        for (CALayer *layer in self.view.layer.sublayers) {
            layer.frame=CGRectOffset(layer.frame, 0, 20);
            if (layer.position.y>528) {
                layer.position=CGPointMake(layer.position.x, 528);
            }
        }
    }
   
}

@end
